package caddey.jonathan.flier;

import framework.Color;
import framework.GameContext;
import framework.GraphicsWrapper;
import framework.KInput;
import framework.Screen;
import gamebackend.GameSession;

public class JoystickOverlay extends Screen {
	private static final int CONTROL_WIDTH = 160;
	private static final int JOY_WIDTH = CONTROL_WIDTH * 100 / 225;
	private static final int CONTROLS_FADE_TIME = 1000;
	
	private GameSession my_gameSession;
	
	private int my_controlsX;
	private int my_controlsY;
	private double my_joyVeloX;
	private double my_joyVeloY;
	private double my_joyX;
	private double my_joyY;
	
	private boolean my_touchDown;
	private double my_pointerVeloX;
	private double my_pointerVeloY;
	/**
	 * 0 means invisible controls; CONTROL_FADE_TIME is fully visible controls.
	 */
	private int my_controlsFadeTimer = CONTROLS_FADE_TIME;

	public JoystickOverlay(final GameContext the_game) {
		super(the_game);
		my_gameSession = new GameSession(the_game);
	}
	
	
	@Override
	public void step(int the_deltaTime) {
		KInput input = my_game.getInput();
		if (input.isTouchDown(0)) {
			if (!my_touchDown) {
				my_touchDown = true;
			}
			final int dx = input.getTouchX(0)
					- (my_controlsX + CONTROL_WIDTH / 2);
			final int dy = input.getTouchY(0)
					- (my_controlsY + CONTROL_WIDTH / 2);
			if (dx * dx + dy * dy < CONTROL_WIDTH * CONTROL_WIDTH / 4 * 1.25 * 1.25) {
				my_pointerVeloX = .25 * my_pointerVeloX + .75 * (dx - my_joyX) / the_deltaTime;
				my_pointerVeloY = .25 * my_pointerVeloX + .75 * (dx - my_joyY) / the_deltaTime;
				
				my_controlsFadeTimer -= the_deltaTime;
				
			} else {
				my_controlsFadeTimer = CONTROLS_FADE_TIME;
			}
			my_joyX = dx;
			my_joyY = dy;
			
			
//		} else {
//			my_inner.setDesiredVelocityRatio(0);
//			my_controlsFadeTimer += the_deltaTime;
//		}
		} else {
			if (my_touchDown) {
				my_touchDown = false;
				my_joyVeloX += my_pointerVeloX;
				my_joyVeloY += my_pointerVeloY;
				my_pointerVeloX = 0;
				my_joyVeloX = 0;
				my_pointerVeloY = 0;
				my_joyVeloY = 0;
			}
		}
		final double current = my_joyY * my_joyY + my_joyX * my_joyX;
		final double ratio = Math.sqrt(current) * 2 / CONTROL_WIDTH;
		if (ratio > 1) {
			System.out.println("ratio " + ratio);
			my_joyX /= ratio;
			my_joyY /= ratio;
			my_pointerVeloX = 0;
		}
		final double velo = 2 * my_joyX / (CONTROL_WIDTH * 4 / 5);
		my_gameSession.setDesiredVelocityRatio(velo);
		
		if (my_joyY < -CONTROL_WIDTH / 5) {
			my_gameSession.toggleLaser(true);
			my_gameSession.toggleLaser(false);
		}
		my_controlsFadeTimer = Math.max(0,
				Math.min(CONTROLS_FADE_TIME, my_controlsFadeTimer));
		my_gameSession.step(the_deltaTime);
		if (!my_touchDown) {
			if (my_joyX != 0) {
				double sign = (int) Math.signum(my_joyX);
				my_joyVeloX -= 10 * sign * the_deltaTime / 100 / 10;
				my_joyX += my_joyVeloX * the_deltaTime;
				if (Math.signum(my_joyX) != sign) {
					my_joyX = 0;
					my_joyVeloX = 0;
				}			
			}
			if (my_joyY != 0) {
				double sign = (int) Math.signum(my_joyY);
				my_joyVeloY -= 10 * sign * the_deltaTime / 100 / 10;
				my_joyY += my_joyVeloY * the_deltaTime;
				if (Math.signum(my_joyY) != sign) {
					my_joyY = 0;
					my_joyVeloY = 0;
				}			
			} 
		}
	}
	

	@Override
	public void render() {
		GraphicsWrapper g = my_game.getGraphicsWrapper();
		g.saveMatrix();
		my_gameSession.render();
		g.restoreMatrix();
		drawControls(g);
	}
	private void drawControls(final GraphicsWrapper the_g) {
		final double ratio = 1;//(double) my_controlsFadeTimer / CONTROLS_FADE_TIME
				//* (double) my_controlsFadeTimer / CONTROLS_FADE_TIME;
		the_g.setColor(new Color(80, 80, 80,
				40 + (int) (180 * (ratio))));
		my_controlsY = 0;//the_g.getHeight() - CONTROL_WIDTH * 3 / 4;
		my_controlsX = CONTROL_WIDTH / 8;
		the_g.fillOval(my_controlsX, my_controlsY,
				CONTROL_WIDTH, CONTROL_WIDTH);
		the_g.setColor(new Color(100, 100, 100,
				40 + (int) (180 * (ratio))));
		the_g.fillOval((int) (my_controlsX + CONTROL_WIDTH / 2 + my_joyX - JOY_WIDTH / 2),
				(int) (my_controlsY + CONTROL_WIDTH / 2 + my_joyY - JOY_WIDTH / 2), JOY_WIDTH, JOY_WIDTH);
		
	}


	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}
		
}


